﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Manager_Menu_Controls_UC_SuaMenu : System.Web.UI.UserControl
{
    BUS_Menu data = new BUS_Menu();
    DTO_Menu obj = new DTO_Menu();
    static DataTable tbMenu = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            string IdMenu = Request.QueryString["id_menu"];
            tbMenu = data.Menu_GetByIdAllStatus(IdMenu);
            //lblID.Text = tbMenu.Rows[0]["Id"].ToString();
            txtTenmenu.Text = tbMenu.Rows[0]["Name"].ToString();
            txtLink.Text = tbMenu.Rows[0]["Link"].ToString();
            txtVitrihientai.Text = tbMenu.Rows[0]["Location"].ToString();
            if (tbMenu.Rows[0]["Status"].ToString() == "True")
            {
                RdMo.Checked = true;
                RdDong.Checked = false;
            }
            else
            {
                RdMo.Checked = false;
                RdDong.Checked = true;
            }
        }
        

    }
    protected void drlVTHT_SelectedIndexChanged(object sender, EventArgs e)
    {
        string IdMenu = Request.QueryString["id_menu"];
        if (drlVTHT.SelectedItem.Value == "1")
        {
            lblMenucha.Visible = false;
            drlMenucha.Visible = false;
            //Table1.Visible = true;
        }
        else
        {
            lblMenucha.Visible = true;
            drlMenucha.Visible = true;
            drlMenucha.DataSource = data.getMenu(2);
            drlMenucha.DataTextField = "Name";
            drlMenucha.DataValueField = "Id";
            drlMenucha.DataBind();
            drlMenucha.Items.Insert(0, "Mặc định");
            if (drlMenucha.SelectedValue == "Mặc định")
            {
                drlMenucha.SelectedItem.Value = "0";
            }
            drlMenucha.Items.Remove(drlMenucha.Items.FindByText(txtTenmenu.Text));

        }
    }
    protected void btnSuaMenu_Click(object sender, EventArgs e)
    {
        //Sua();
        obj.Name = txtTenmenu.Text;
        obj.Link = txtLink.Text;
        obj.Location = int.Parse(drlVTHT.SelectedItem.Value);
        if (RdMo.Checked == true && RdDong.Checked == false)
        {
            obj.Status = true;
        }
        else
        {
            obj.Status = false;
        }
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(Server.MapPath("~/Images/" + FileUpload1.FileName));
            //obj.Img = FileUpload1.FileName;
        }
        if (FileUpload1.FileName.Length == 0)
        {
            obj.Img = tbMenu.Rows[0]["Img"].ToString();
        }
        else
        {
            obj.Img = FileUpload1.FileName; 
        }
        if (drlVTHT.SelectedItem.Value == "1")
        {
            //if (FileUpload1.HasFile)
            //{
            //    FileUpload1.SaveAs(Server.MapPath("~/Images/" + FileUpload1.FileName));
            //    obj.Img = FileUpload1.FileName;
            //}
            obj.Parent_Id = 0;

        }
        else
        {
            obj.Img = "";
            obj.Parent_Id = int.Parse(drlMenucha.SelectedItem.Value);
        }
        string IdMenu = Request.QueryString["id_menu"];
        data.Menu_UpdateByAll(IdMenu, obj);
        Response.Redirect("Menu.aspx");
        //Response.Write("drlMenucha.Items.Remove("+txtTenmenu.Text+")");

    }

    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Response.Redirect("Menu.aspx");
    }
}